<?php

require_once(WSB_MODULES_PATH . 'sherpa/models/DbTable/Store.php');

class Sherpa_StoresController extends WSB_Xtension_Extension
{

	/**
	 * @return string
	 */
	public function renderForBackend()
	{
		return get_class($this);
	}

	/**
	 * Index Action
	 */
	public function indexAction()
	{
        // API-Key für sherpaoutdoor.ch:
        // ABQIAAAA5jwO-b0I6sPgY5QDlfF9aRTK0O1XRgPV40203v0XyqIXm4Q-BRQFAgNxon5NesVEfjXrKot32LL1Mg

        $this->_view->headScript()->appendFile('http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA5jwO-b0I6sPgY5QDlfF9aRTK0O1XRgPV40203v0XyqIXm4Q-BRQFAgNxon5NesVEfjXrKot32LL1Mg');
        $javascript = $this->_view->render('stores/index.js.phtml');
        $this->_view->headScript()->appendScript($javascript, 'text/javascript');

        $table = new Sherpa_Model_DbTable_Store();
        $this->_view->groupedOptions = $table->getOptionsGroupedByType();
        $this->_view->selected = 0;

		$this->addContent($this->_view->render('stores/index.phtml'));
	}

    public function storeAction()
    {
        $id = intval($this->getParam('id'));
        if($id > 0) {
            $table = new Sherpa_Model_DbTable_Store();

            $select = $table->select();
            $select->where('deleted=?', 0);
            $select->where('hidden=?', 0);
            $select->where('id=?', $id);
            $store = $table->fetchRow($select);

            $this->_view->groupedOptions = $table->getOptionsGroupedByType();
            $this->_view->selected = $id;

    		$this->_view->rows = array($store);
            $xmlData = $this->_view->render('stores/xml.phtml');
            $this->_view->xml = str_replace(array("\n", "\r"), '', $xmlData);

            $this->_view->store = $store;

            if(empty($store)) {
                $this->addContent($this->_view->render('stores/error.phtml'));
                return;
            }

            $this->addContent($this->_view->render('stores/store.phtml'));

            $this->_view->headScript()->appendFile('http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA5jwO-b0I6sPgY5QDlfF9aRTK0O1XRgPV40203v0XyqIXm4Q-BRQFAgNxon5NesVEfjXrKot32LL1Mg');
            $javascript = $this->_view->render('stores/store.js.phtml');
            $this->_view->headScript()->appendScript($javascript, 'text/javascript');
        }
    }

    public function xmlAction()
    {

		$table = new Sherpa_Model_DbTable_Store();
		$select = $table->select();
		$select->where('deleted=?', 0);
		$select->where('hidden=?', 0);
		$this->_view->rows = $table->fetchAll($select);
        $this->_view->showLink = true;

        header("HTTP/1.1 200 OK");
        header("Content-type: text/xml");
        echo $this->_view->render('stores/xml.phtml');
        exit;
    }

}